package com.xing.crm.service.impl;

import com.xing.crm.mapper.ProShopMapper;
import com.xing.crm.pojo.PageResult;
import com.xing.crm.pojo.ProShop;
import com.xing.crm.pojo.WebUser;
import com.xing.crm.service.ProRoleService;
import com.xing.crm.service.ProShopService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mr.L
 * @version V1.0
 * @date 2021年08月05日 14:51
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ProShopServiceImpl implements ProShopService
{
	
	@Resource
	private ProShopMapper proShopMapper;
	@Resource
	private ProRoleService proRoleService;
	
	@Override
	public PageResult<ProShop> list(int page, int limit, ProShop shop)
	{
		Map<String, Object> params = new HashMap(0);
		params.put("start", (page - 1) * limit);
		params.put("pageSize", limit);
		params.put("shop", shop);
		List<ProShop> shops = proShopMapper.list(params);
		
		PageResult<ProShop> result = new PageResult();
		result.setCode(0);
		result.setCount(proShopMapper.count(params));
		result.setMsg("查询成功");
		result.setData(shops);
		
		return result;
	}
	
	@Override
	public Map<String, Object> edit(ProShop shop)
	{
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "保存成功");
		if(shop.getId() == null)
		{
			int id = proShopMapper.insert(shop);
			shop.setId(id);
		}
		else
		{
			proShopMapper.edit(shop);
		}
		result.put("data", shop);
		return result;
	}
	
	@Override
	public ProShop get(Integer id)
	{
		ProShop shop = null;
		if(id != null) {
			shop = proShopMapper.get(id);
		}
		return shop;
	}
	
	@Override
	public PageResult<ProShop> getShops() {
		List<ProShop> shops = proShopMapper.getShops();
		PageResult<ProShop> result = new PageResult();
		result.setCode(0);
		result.setMsg("查询成功");
		result.setData(shops);
		result.setCount(shops.size());
		return result;
	}
	
	@Override
	public Map<String, Object> delete(Integer id)
	{
		Map<String, Object> result = new HashMap(0);
		result.put("code", 0);
		result.put("msg", "执行成功");
		int success = 0;
		if(id != null)
		{
			ProShop shop = proShopMapper.get(id);
			if(!StringUtils.isEmpty(shop.getCover()))
			{
				/* 删除店铺图片 */
				String dir = System.getProperty("user.dir") + File.separator;
				File file = new File(dir, shop.getCover());
				if(file.exists())
				{
					file.delete();
				}
			}
			/* 店铺所有店员账号id */
			List<Integer> uids = proShopMapper.getShopUids(id);
			if(!uids.isEmpty()) {
				for(Integer uid : uids) {
					/* 删除店铺关联用户角色 */
					proRoleService.deleteShopUsersRole(uid);
				}
			}
			/* 删除店铺关联产品 */
			proShopMapper.deleteShopProducts(id);
			/* 删除店铺关联用户 */
			proShopMapper.delAssociateData(id);
			/* 删除店铺 */
			success = proShopMapper.delete(id);
		}
		result.put("data", success);
		return result;
	}
	
	@Override
	public ProShop getShopByUid(WebUser webUser) {
		ProShop shop = null;
		if(webUser != null) {
			shop = proShopMapper.getShopByUid(webUser.getId());
		}
		return shop;
	}
	
	@Override
	public PageResult<Map<String, Object>> getShopClerks(ProShop shop, Boolean isManager) {
		PageResult<Map<String, Object>> result = new PageResult();
		result.setMsg("权限不足");
		result.setCode(0);
		if(isManager != null && isManager && shop != null) {
			List<Map<String, Object>> clerks = proShopMapper.getShopClerks(shop.getId());
			result.setData(clerks);
			result.setCount(clerks.size());
		}
		return result;
	}
	
	@Override
	public PageResult delShopUser(Integer uid, Integer sid, WebUser user, Boolean isManager) {
		PageResult result = new PageResult();
		result.setCode(-1);
		result.setMsg("没有权限");
		/* 是否店长 */
		if(isManager) {
			if(uid != null && sid != null && user != null) {
				if(uid.equals(user.getId())) {
					result.setMsg("不能剔除自己");
				} else {
					/* 删除角色关联 */
					proRoleService.delUserRole(uid);
					/* 删除店铺关联 */
					proShopMapper.delShopUser(uid, sid);
					result.setCode(0);
					result.setMsg("操作成功");
				}
			}
		}
		return result;
	}
}
